
let Url_Config = "http://192.168.3.104:9000";


//#ifdef MP-WEIXIN
const accountInfo = uni.getAccountInfoSync().miniProgram;
export const {
	appId,
	envVersion,
	version
} = accountInfo;
 
switch (envVersion) {
	case "develop":
	case "trial":
		Url_Config = "http://192.168.3.138:8090";
		//"http://192.168.3.104:9000";
		break;
	case "release":
		Url_Config = "http://192.168.3.104:9000";
		break;
	default:
		break;
}
//#endif


export const baseUrl = Url_Config;

export const getToken = () => {
	return new Promise((resolve, _) => {
		uni.getStorage({
			key: "token",
			success: (res) => {
				resolve(res.data);
			},
			fail: () => {
				resolve("");
			},
		});
	});
};

export async function request({
	method,
	url,
	data = {},
	header = null,
	timeout = 10000,
	skipLogin = false,
	skipError = false,
	_baseUrl=Url_Config
}) {
	console.log(`${_baseUrl}${url}`, "请求链接");
	const token = await getToken();
	
	if(!header)
	{
		header={}
		header["Content-Type"] = "application/json"
		header["Authorization"] = token;
		// #ifdef MP-ALIPAY
		header["app-client-type"] = "alipay_mini_app";
		// #endif
		// #ifdef MP-WEIXIN
		header["app-client-type"] = "wx_mini_app";
		// #endif
	}
	
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${_baseUrl}${url}`,
			data,
			header,
			timeout,
			method,
			success: (res) => {
				let resData = res.data
				//console.log(resData, "正确的返回参数");
				if (resData.code != 200 && !skipError) {
					uni.$emit('showToast',{
						title: resData.msg, 
					});
				}
				 
				resolve(resData);
			},
			fail: (req) => {
				uni.showToast({
					title: "网络发生错误",
					icon: "none"
				});
				reject(req);
			},
			finally: () => {
				uni.hideLoading();
			},
		});
	});
}